package com.trex.modules.tbl.workerplace.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.place.entity.TblZdZycs;
import com.trex.modules.tbl.place.service.TblZdZycsService;
import com.trex.modules.tbl.workerplace.entity.TblZdWorkerdz;
import com.trex.modules.tbl.workerplace.service.TblZdWorkerdzService;

/**
 * 人员场所对照Controller
 * @author jy
 * @version 2022-12-09
 */
@Controller
@RequestMapping(value = "${adminPath}/tbl/workerplace/tblZdWorkerdz")
public class TblZdWorkerdzController extends BaseController {

	@Autowired
	private TblZdWorkerdzService tblZdWorkerdzService;
	
	@Autowired
	private TblZdZycsService tblZdZycsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TblZdWorkerdz get(String recId, boolean isNewRecord) {
		return tblZdWorkerdzService.get(recId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:workerplace:tblZdWorkerdz:view")
	@RequestMapping(value = {"list", ""})
	public String list(TblZdWorkerdz tblZdWorkerdz, Model model) {
		model.addAttribute("tblZdWorkerdz", tblZdWorkerdz);
		List<TblZdZycs> placeList = tblZdZycsService.findList(new TblZdZycs());
		placeList.add(0, new TblZdZycs());
		model.addAttribute("placeList", placeList);
		return "modules/tbl/workerplace/tblZdWorkerdzList";
	}
	
	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:workerplace:tblZdWorkerdz:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TblZdWorkerdz> listData(TblZdWorkerdz tblZdWorkerdz, HttpServletRequest request, HttpServletResponse response) {
		tblZdWorkerdz.setPage(new Page<>(request, response));
		Page<TblZdWorkerdz> page = tblZdWorkerdzService.findPage(tblZdWorkerdz);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:workerplace:tblZdWorkerdz:view")
	@RequestMapping(value = "form")
	public String form(TblZdWorkerdz tblZdWorkerdz, Model model) {
		model.addAttribute("tblZdWorkerdz", tblZdWorkerdz);
		List<TblZdZycs> placeList = tblZdZycsService.findList(new TblZdZycs());
		placeList.add(0, new TblZdZycs());
		model.addAttribute("placeList", placeList);
		return "modules/tbl/workerplace/tblZdWorkerdzForm";
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:workerplace:tblZdWorkerdz:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TblZdWorkerdz tblZdWorkerdz, HttpServletRequest request) {
		String I_IP_ADDR = request.getRemoteHost(); // 取IP
		String I_USERCODE = UserUtils.getUser().getUserCode(); // 取用户id
		String I_USERNAME = UserUtils.getUser().getUserName(); //用户名
		
		tblZdWorkerdz.setIp(I_IP_ADDR);
		tblZdWorkerdz.setUserCode(I_USERCODE);
		tblZdWorkerdz.setUserName(I_USERNAME);
		tblZdWorkerdz.setUpdTime(new Date());
		tblZdWorkerdz.setDeleFlag("0");
		
		TblZdWorkerdz tblZdWorkerdz_new = new TblZdWorkerdz();
		tblZdWorkerdz_new.setCsbh(tblZdWorkerdz.getCsbh());
		tblZdWorkerdz_new.setWorkerId(tblZdWorkerdz.getWorkerId());
		//tblZdWorkerdz_new.setWorkerName(tblZdWorkerdz.getWorkerName());
		
		if(tblZdWorkerdz.getIsNewRecord()) {
			//新增 判断记录是否已存在
			if(tblZdWorkerdzService.findCount(tblZdWorkerdz_new) > 0) {
				return renderResult(Global.FALSE, text("对照信息已存在，无法新增！"));
			};
		}else {
			//修改
			List<TblZdWorkerdz> listCS = tblZdWorkerdzService.findList(tblZdWorkerdz_new);

			for(int a=0;a<listCS.size();a++) {
				TblZdWorkerdz tblZdWorkerdz_new2 = listCS.get(a);
				if(tblZdWorkerdz_new2.getRecId().equals(tblZdWorkerdz.getRecId())) {
					continue;
				}else {
					return renderResult(Global.FALSE, text("对照信息已存在，修改失败！"));
				}
			}
		}
		
		tblZdWorkerdzService.save(tblZdWorkerdz);
		return renderResult(Global.TRUE, text("保存人员场所对照成功！"));
	}
	
	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:workerplace:tblZdWorkerdz:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TblZdWorkerdz tblZdWorkerdz) {
		tblZdWorkerdzService.delete(tblZdWorkerdz);
		return renderResult(Global.TRUE, text("删除人员场所对照成功！"));
	}
	
}